import 'dart:convert';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:provider/provider.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBBaseRouter/YBApplication.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBBaseRouter/YBNavigator.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBWeChatPopPage/YBCameraSelectPopPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBWeChatPopPage/YBImageEventBus.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBColor/YBColor.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBImageStyle/YBImageStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBTextStyle/YBTextStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/yb_chinese_text/yb_chinese_text.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBMineModule/YBMineProvider/YBMineUserProvider.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBMineModule/YBQiYePersonPage/YBNumberManagerProvider/YBYBImageProvider.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBAPi/YBAPi.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBBaseMineNetWork/YBBaseMineNetWork.dart';

import 'YBNumberManagerProvider/YBQiYeAndPersonManagerEventBus.dart';

class YBPersonMainPage extends StatefulWidget {
  @override
  _YBPersonMainPageState createState() => _YBPersonMainPageState();
}

class _YBPersonMainPageState extends State<YBPersonMainPage> {

  String _xingmingStr;
  String _idNoStr;
  
  String _idbackUrlStr;
  String _idFontUrlStr;

  bool _backUrlBool = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    qiYeAndPersonManagerEventBus.on<YBQiYeAndPersonManagerEventBus>().listen((event) {
       if (event.title.contains('姓名')){
         setState(() {
           _xingmingStr = event.content;
         });

       }else if (event.title.contains('身份证号')){
         setState(() {
           _idNoStr = event.content;
         });

       }
    });


    imageStrEventBus.on<YBImageEventBus>().listen((event) {
      if (_backUrlBool == false){
        print('-----${event.imageStr}-');
        setState(() {
          _idFontUrlStr = event.imageStr;
        });
      }
    });

    imageStrEventBus.on<YBImageEventBus>().listen((event) {
      if (_backUrlBool == true){
        print('---反面--${event.imageStr}-');
        setState(() {
          _idbackUrlStr = event.imageStr;
        });
      }
    });
    
  }

  @override
  void dispose() {
    // TODO: implement dispose
    qiYeAndPersonManagerEventBus.destroy();
    imageStrEventBus.destroy();
    super.dispose();
  }


  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        appBar: AppBar(
          elevation: 0,
          title: Text(
            YBChineseText.minePersonAuthTitle,
            style: YBTextStyle.TextStyle30313320Medium
          ),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Expanded(
              child: ChangeNotifierProvider(
                create:(context) => YBMineUserProvider()..getUserInfoData(),
                child: Consumer<YBMineUserProvider>(
                    builder:(context,userInfo,child){
                      String _xingMingContainerStr = '';
                      String _idNoContainerStr = '';
                      String _idbackUrlContainerStr;
                      String _idFontUrlContainerStr;
                      if (!ObjectUtil.isEmpty(userInfo.mineUserInfoModel)) {
                        if (!ObjectUtil.isEmpty(userInfo.mineUserInfoModel.idDetail)) {
                          _idNoContainerStr = _idNoStr??userInfo.mineUserInfoModel.idDetail.idNo;
                          _idbackUrlContainerStr  = _idbackUrlStr??userInfo.mineUserInfoModel.idDetail.idBackUrl;
                          _idFontUrlContainerStr = _idFontUrlContainerStr??userInfo.mineUserInfoModel.idDetail.idFrontUrl;
                        }
                        _xingMingContainerStr = _xingmingStr??userInfo.mineUserInfoModel.customerName;
                      }
                      return SingleChildScrollView(
                        child: Column(
                          children: [
                            Container(
                              height: 8,
                            ),
                            Container(
                              color: YBColor.white,
                              child: Padding(
                                padding: EdgeInsets.only(left:YBTextStyle.adapterSize20,right:YBTextStyle.adapterSize20),
                                child: Column(
                                  children: [
                                    Container(
                                      child: InkWell(
                                        child: getPersonLoadInfo(YBChineseText.mineXingMingTitle, _xingMingContainerStr??""),
                                        onTap: (){
                                          YBApplication.router.navigateTo(context, '/mine/mineQiYePersonPage/mineQuanCengHomePage/mineQiYePersonEditingPage?titleHeader=${Uri.encodeComponent("姓名")}&&contentTitle=${Uri.encodeComponent(_xingMingContainerStr)}');
                                        },
                                      ),
                                    ),
                                    Container(
                                      child: InkWell(
                                        child: getPersonLoadInfo(YBChineseText.mineShengfengHaoTitle, _idNoContainerStr??""),
                                        onTap: (){
                                          YBApplication.router.navigateTo(context, '/mine/mineQiYePersonPage/mineQuanCengHomePage/mineQiYePersonEditingPage?titleHeader=${Uri.encodeComponent("身份证号")}&&contentTitle=$_idNoContainerStr');
                                        },
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                            Container(
                              height: YBTextStyle.adapterSize10,
                            ),
                            Container(
                              height: YBTextStyle.adapterSize60,
                              color: YBColor.white,
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.start,
                                crossAxisAlignment: CrossAxisAlignment.stretch,
                                children: [
                                  Container(
                                    height: 10,
                                  ),
                                  Container(
                                    child: Padding(
                                      padding: EdgeInsets.only(left: YBTextStyle.adapterSize20,right: YBTextStyle.adapterSize20),
                                      child: Text(
                                        YBChineseText.mineShengFengZhengZhaoPianTitle,
                                        style: YBTextStyle.TextStyle2F303314,
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            Container(
                              height: YBTextStyle.adapterSize15,
                              color: YBColor.white,
                            ),
                            Container(
                              height: YBTextStyle.adapterSize70,
                              color: YBColor.white,
                              child: Padding(
                                padding:EdgeInsets.only(left: YBTextStyle.adapterSize20,right: YBTextStyle.adapterSize20),
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: [
                                    Expanded(
                                      child: Image.asset(YBImageStyle.getImagePath('shenfenzheng_siliimageView'),fit:BoxFit.contain,),
                                    ),
                                    Container(
                                      width: YBTextStyle.adapterSize20,
                                    ),
                                    Expanded(
                                      child: InkWell(
                                        child: CachedNetworkImage(imageUrl:"${YBApi.Base_image_URL}/$_idFontUrlContainerStr",errorWidget: (context,url,error)=>Image.asset(YBImageStyle.getImagePath("authjiahao")),),
                                        onTap: () async {
                                          _backUrlBool = false;
                                          YBCameraSelectPopPage.getCameraSelectPop(context);
                                        },
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                            Container(
                              color: YBColor.white,
                              height: YBTextStyle.adapterSize10,
                            ),
                            Container(
                              color: YBColor.white,
                              child:Row(
                                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  Expanded(
                                    child: Center(
                                      child: Text(
                                        YBChineseText.minePersonZhengMianShiLiTuTitle,
                                        style: YBTextStyle.TextStyleA9AEB812,
                                      ),
                                    ),
                                  ),
                                  Container(
                                    width: YBTextStyle.adapterSize20,
                                  ),
                                  Expanded(
                                    child:Center(
                                      child:  Text(
                                        YBChineseText.minePersonuploadZhengMianShiLiTuTitle,
                                        style: YBTextStyle.TextStyleA9AEB812,
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            Container(
                              color: YBColor.white,
                              height: YBTextStyle.adapterSize15,
                            ),
                            Container(
                              height: YBTextStyle.adapterSize60,
                              color: YBColor.white,
                              child: Padding(
                                padding:EdgeInsets.only(left: YBTextStyle.adapterSize20,right: YBTextStyle.adapterSize20),
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: [
                                    Expanded(
                                      child: Image.asset(YBImageStyle.getImagePath('shengfenzheng_silibeimian'),fit:BoxFit.contain,),
                                    ),
                                    Container(
                                      width: YBTextStyle.adapterSize20,
                                    ),
                                    Expanded(
                                      child: InkWell(
                                        child: CachedNetworkImage(imageUrl:"${YBApi.Base_image_URL}/$_idbackUrlContainerStr",errorWidget: (context,url,error)=>Image.asset(YBImageStyle.getImagePath("authjiahao")),),
                                        onTap: () async {
                                          _backUrlBool = true;
                                          YBCameraSelectPopPage.getCameraSelectPop(context);
                                        },
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                            Container(
                              color: YBColor.white,
                              height: YBTextStyle.adapterSize10,
                            ),
                            Container(
                              color: YBColor.white,
                              child:Row(
                                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  Expanded(
                                    child: Center(
                                      child: Text(
                                        YBChineseText.minePersonFanMianShiLiTuTitle,
                                        style: YBTextStyle.TextStyleA9AEB812,
                                      ),
                                    ),
                                  ),
                                  Container(
                                    width: YBTextStyle.adapterSize20,
                                  ),
                                  Expanded(
                                    child: Center(
                                      child: Text(
                                        YBChineseText.minePersonuploadFanMianShiLiTuTitle,
                                        style: YBTextStyle.TextStyleA9AEB812,
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                      );
                    }
                ),
              ),
            ),
            Container(
              height: YBTextStyle.adapterSize45,
              child: Padding(
                padding: EdgeInsets.only(left: YBTextStyle.adapterSize20,right: YBTextStyle.adapterSize20),
                child: RawMaterialButton(
                  child: Text(YBChineseText.mineTiJiAuthTitle,style: YBTextStyle.TextStyleFFFFFF14Sembold,),
                  fillColor: YBColor.commonThemeColor,
                  shape:RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(YBTextStyle.adapterSize6),
                  ),
                  onPressed: () async {
                    _editingTiJiao(_xingmingStr, _idNoStr, _idFontUrlStr, _idbackUrlStr);
                    YBNavigatorUtil.goBack(context);
                  },
                ),
              ),
            ),
            Container(
              height: YBTextStyle.adapterSize20,
            ),
          ],
        ),
      ),
    );
  }

  Widget getPersonLoadInfo(String title, String bodyTitle){
    return Container(
      height: YBTextStyle.adapterSize58,
      child: Row(
        children: [
          Container(
            child: Text(
              title,
              style: YBTextStyle.TextStyle2F303314,
            ),
          ),
          Expanded(
            child: Container(
              alignment: Alignment.centerRight,
              child: Text(
                bodyTitle,
                style: YBTextStyle.TextStyle2F303314,
              ),
            ),
          ),
          Container(
            width: YBTextStyle.adapterSize5,
          ),
          Container(
            child: Image.asset(YBImageStyle.getImagePath('findIconArrowRight')),
          ),
        ],
      ),
    );
  }

  void _editingTiJiao(String certLinkName, String idNo,String idFrontUrll,String idBackUrl){
    Map<String, dynamic> editingMap = Map();
    editingMap['sysCode'] = "xg_youbang";
    editingMap['certStatus'] = -1;
    if (certLinkName != null) {
      editingMap['customerName'] = certLinkName;
    }
    Map<String, dynamic> idDetail = Map<String,dynamic>();
    if (idNo != null) {
      idDetail['idNo'] = idNo;
    }
    if (_idFontUrlStr != null) {
      idDetail['idFrontUrl'] = idFrontUrll;
    }
    if (_idbackUrlStr != null) {
      idDetail['idBackUrl'] = idBackUrl;
    }
    editingMap['idDetail'] = jsonEncode(idDetail);

    YBMineNetWork.mineQiYeAndPersonEditingData(editingMap,success:(data){
      print('---$data-');
      EasyLoading.showInfo('上传成功');
    },fail: (error){
      print('---$error-');
    });
  }


}

